package com.mobi.Survey.controller;

import java.util.List;

import android.content.Context;

import com.mobi.Survey.model.AbstractTable;
import com.mobi.Survey.model.SurveyResultTable;
import com.mobi.Survey.uicomponents.ControlFactory;

/**
 * 
 * <b>Author:</b> Yahya Arshad
 * <p>
 * <b>Company:</b> Mobi Media Soft
 * <p>
 * <b> Website:</b> www.mobimediasoft.com
 * <p>
 * SurveyResultPojo.java Used to hold Survey Results
 */
public class SurveyResultPojo {

	private SurveyDetailPojo surveyResult;

	public SurveyDetailPojo getSurveyResult() {
		return surveyResult;
	}

	public void setSurveyResult(SurveyDetailPojo surveyResult) {
		this.surveyResult = surveyResult;
	}

	public int isNew(int id) {

		return id;

	}

	/**
	 * Weather user has already answered this survey if user already answered
	 * this survey it is not new
	 * 
	 * @param surveyId
	 *            id of survey for which being answered
	 * @param ctx
	 * @return value greater than 0 if already answered
	 */
	public static int isNew(long surveyId, Context ctx) {
		AbstractTable at = new SurveyResultTable(ctx);
		int total = ((SurveyResultTable) at).isNew(surveyId);
		at.closeDB();
		return total;

	}

	/**
	 * writes survey answers from controls or database to the survey detail
	 * passed to it
	 * 
	 * @param surveyId
	 * @param surveyDetails
	 *            collection to which write answers
	 * @param ctx
	 */
	public static void writeSurveyResult(long surveyId,
			List<SurveyDetailPojo> surveyDetails, Context ctx) {
		AbstractTable at = new SurveyResultTable(ctx);
		((SurveyResultTable) at).writeResult(surveyId, surveyDetails);
		at.closeDB();

	}

	/**
	 * delete survey result for given survey id
	 * 
	 * @param surveyId
	 * @param ctx
	 */
	public static void delete(long surveyId, Context ctx) {
		AbstractTable at = new SurveyResultTable(ctx);
		((SurveyResultTable) at).deleteRow(surveyId);
		at.closeDB();
	}

	/**
	 * save a survey result in database
	 * 
	 * @param surveyId
	 *            id of survey to which survey result belongs
	 * @param surveyDetails
	 * @param ctx
	 */
	public static void commit(long surveyId,
			List<SurveyDetailPojo> surveyDetails, Context ctx) {

		AbstractTable at = new SurveyResultTable(ctx);

		for (SurveyDetailPojo sdp : surveyDetails) {

			if (sdp.getControlType() != ControlFactory.CONTROL_IMAGE_VIEW
					&& sdp.getControlType() != ControlFactory.CONTROL_VIDEO_VIEW
					&& sdp.getControlType() != ControlFactory.CONTROL_TYPE_ZERO
					&& sdp.getControlType() != ControlFactory.CONTROL_URL)
				at.insertRow(sdp, surveyId);
		}
		at.closeDB();

	}
}
